﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.ESS.BLL;
//using KPIS.GERP.GFMS.IBLL;
//using KPIS.GERP.GFMS.BLL;
//using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class LoginOld : System.Web.UI.Page
    {
        //HRM_PS_Employees.HRM_PS_Employees emp = new HRM_PS_Employees.HRM_PS_Employees();
        //Security.Security security = new Security.Security();
        private B_SECURITY_Users userBLL = new B_SECURITY_Users();
        FIM_SystemControls.FIM_SystemControls sc = new FIM_SystemControls.FIM_SystemControls();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string strUserID = txtUserID.Text.ToUpper().Trim();
            string strPassword = txtPassword.Text.Trim();

            if (strUserID.Length == 0)
            {
                lblError.Text = "กรุณากรอก [รหัสประจำตัว]";
                return;
            }

            if (!CheckPassword(strUserID, strPassword))
            {
                lblError.Text = "[รหัสผ่าน] ไม่ถูกต้อง";
                return;
            }
            else
            {
                Response.Redirect(@"~\App_UI\Forms\SelectSystem.aspx");
            }
        }

        private bool CheckPassword(string strUserID, string strPassword)
        {
            bool bOK = false;
            //DataSet ds = security.Login(strUserID, strPassword);
            DataTable dt = userBLL.GetUserInfo(strUserID, strPassword);

            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                Session["EmpSeq"] = dr["emp_seq"].ToString();
                Session["UserSeq"] = dr["user_seq"].ToString();
                Session["WorkingJobSiteSeq"] = dr["working_job_site_seq"].ToString();
                Session["WorkingJobSiteName"] = dr["working_job_site_name"].ToString();
                Session["WorkAreaSeq"] = dr["work_area_seq"].ToString();
                //Session["UserName"] = dr["user_name"].ToString();

                HttpCookie cookie = new HttpCookie("UserDetail");
                cookie["EmpSeq"] = dr["emp_seq"].ToString();
                cookie["UserName"] = dr["user_name"].ToString();
                cookie["UserSeq"] = dr["user_seq"].ToString();
                cookie["WorkingJobSiteSeq"] = dr["working_job_site_seq"].ToString();
                Response.Cookies.Add(cookie);
                SetSystemControl();
                bOK = true;
            }

            return bOK;
        }

        private void SetSystemControl()
        {
            Session["SCInfo"] = sc.GetInfo();
        }
    }
}
